<?php
/**
 * LowCalVanilla Page_Unload control
 *
 * @package Extensions
 * @version @@VERSION@@
 * @subpackage Low-Cal_Vanilla
 * @copyright Damien Lebrun 2007
 * @author Damien Lebrun
 * @license http://www.gnu.org/licenses/old-licenses/gpl-2.0.html GPLv2
 */


/**
 * LowCalVanilla Page_Unload control; Get the content from the buffer and compress it.
 * @package Extensions
 * @subpackage Low-Cal_Vanilla
 */
class LowCalVanilla_CompressPage extends Control {

	/**
	 * Constructor
	 * @param Context $Context vanilla context object
	 * @return void
	 */
	function LowCalVanilla_CompressPage(&$Context) {
		$this->Name = 'LowCalVanilla_CompressPage';
		$this->Control($Context);
	}

	/**
	 * Compress buffer content (on the Page_Unload event)
	 * @return void
	 */
	function Render() {
		// This is only defined if gunzip extension is installed
		if (defined('FORCE_GZIP')
			&& !LowCalVanilla_IsGzipIncompatibleClient()
		) {
			// Get the content of the page
			$content = ob_get_contents();
			ob_clean();
			// Create a new output handler inside the main one
			ob_start("ob_gzhandler");
			echo $content;
			// Flush the gunzip content into the main output handler
			// Allow to get ziped content length
			// http://www.php.net/manual/en/function.ob-get-length.php
			ob_end_flush();
			header('Content-Length: ' . ob_get_length());
		}
	}
}
